import sys
sys.path.insert(0,'../')
from RestAPI.amazon.S3 import  S3Request,S3session,Bucket,FileObject
from RestAPI.core.APIExceptions import *
import unittest
import setting

class testS3Request (unittest.TestCase):
    def setUp(self):
        pass
    def testHost(self):
        print "test set host\n===================================================="
        req=S3Request().setHost('http://johnsmith.net:8000')
        print req.host
    def testCanolize(self):
        pass
    def testInitRequest(self):
        print "test initiate request\n===================================================="
        req=S3Request("johnson.s3-en.amazonaws.com")
        print req.__class__
        print req.host
        print req.bucket
    def testSignGET(self):
        print "test signing GET method 1\n===================================================="
        req=S3Request("johnsmith.s3.amazonaws.com").GET("/photos/puppy.jpg")
        req.timestamp=lambda : 'Tue, 27 Mar 2007 19:36:42 +0000'
        req.authorize('AKIAIOSFODNN7EXAMPLE','wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
        print "String to sign:\n===================================================="
        print req.getStringToSign()
        print "Signature:\n===================================================="
        print req.getSignature()
        self.assertEqual(req.getSignature(),"AWS AKIAIOSFODNN7EXAMPLE:bWq2s1WEIj+Ydj0vQ697zp+IXMU=")
        print "test signing GET method 2\n===================================================="
        req=S3Request("johnsmith.s3.amazonaws.com").GET("/").addSubResources({"prefix":"photo",
                                                                              "max-keys":50,
                                                                              "marker":"puppy"})
        req.timestamp=lambda : 'Tue, 27 Mar 2007 19:42:41 +0000'
        req.authorize('AKIAIOSFODNN7EXAMPLE','wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
        print "String to sign:\n===================================================="
        print req.getStringToSign()
        print "Signature:\n===================================================="
        print req.getSignature()
        self.assertEqual(req.getSignature(),"AWS AKIAIOSFODNN7EXAMPLE:htDYFYduRNen8P9ZfE/s9SuKy0U=")
        print "test signing GET method 3\n===================================================="
        req=S3Request("johnsmith.s3.amazonaws.com").GET("/").addSubResource('acl')
        req.timestamp=lambda : 'Tue, 27 Mar 2007 19:44:46 +0000'
        req.authorize('AKIAIOSFODNN7EXAMPLE','wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
        print "String to sign:\n===================================================="
        print req.getStringToSign()
        print "Signature:\n===================================================="
        print req.getSignature()
        self.assertEqual(req.getSignature(),"AWS AKIAIOSFODNN7EXAMPLE:c2WLPFtWHVgbEmeEG93a4cG37dM=")
    def testSignPUT(self):
        print "test signing PUT method 1\n===================================================="
        req=S3Request("johnsmith.s3.amazonaws.com").PUT("/photos/puppy.jpg").addHeaders({'Content-type':'image/jpeg',
                                                                                          'Content-Length':'94328'
                                                                                          })
        req.timestamp=lambda : 'Tue, 27 Mar 2007 21:15:45 +0000'
        req.authorize('AKIAIOSFODNN7EXAMPLE','wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
        print "String to sign:\n===================================================="
        print req.getStringToSign()
        print "Signature:\n===================================================="
        print req.getSignature()
        self.assertEqual(req.getSignature(),"AWS AKIAIOSFODNN7EXAMPLE:MyyxeRY7whkBe+bq8fHCL/2kKUg=")
        print "test signing PUT method 2\n===================================================="
        req=S3Request("static.johnsmith.net:8080").PUT("/db-backup.dat.gz").addHeaders({'x-amz-acl':'public-read',
                                                                                          'content-type':'application/x-download',
                                                                                          'Content-MD5':'4gJE4saaMU4BqNR0kLY+lw==',
                                                                                          'X-Amz-Meta-ReviewedBy':'joe@johnsmith.net',
                                                                                          'X-Amz-Meta-FileChecksum':'0x02661779',
                                                                                          'X-Amz-Meta-ChecksumAlgorithm':'crc32',
                                                                                          'Content-Disposition':'attachment;filename=database.dat',
                                                                                          'Content-Encoding':'gzip',
                                                                                          'Content-Length':5913339
                                                                                          }).addHeader('X-Amz-Meta-ReviewedBy','jane@johnsmith.net')
        req.timestamp=lambda : 'Tue, 27 Mar 2007 21:06:08 +0000'
        req.authorize('AKIAIOSFODNN7EXAMPLE','wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
        print "String to sign:\n===================================================="
        print req.getStringToSign()
        print "Signature:\n===================================================="
        print req.getSignature()
        self.assertEqual(req.getSignature(),"AWS AKIAIOSFODNN7EXAMPLE:ilyl83RwaSoYIEdixDQcA4OnAnc=")
    def testtemporaryUrl(self):
        print "test signing GET url\n===================================================="
        req=S3Request("johnsmith.s3.amazonaws.com").GET("/photos/puppy.jpg")
        req.timestamp=lambda : 'Tue, 27 Mar 2007 19:42:41 +0000'
        req.authorize('AKIAIOSFODNN7EXAMPLE','wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY').expires(1175139620)
        print "String to sign:\n===================================================="
        print req.getStringToSignForUrl()
        print "Signature:\n===================================================="
        print req.getSignatureForUrl(req.getStringToSignForUrl())
        print req.temporaryUrl()
    def testgetForm(self):
        print "test get upload form\n===================================================="
        req=S3Request("johnsmith.s3.amazonaws.com").POST("/")
        req.timestamp=lambda : 'Tue, 27 Mar 2007 19:42:41 +0000'
        req.authorize('AKIAIOSFODNN7EXAMPLE','wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY').expires(1175139620)
#        print req.getForm()
class testS3session(unittest.TestCase):
    def setUp(self):
        self.session=S3session()
        self.session.setCredential(setting.AUTHID,setting.AUTHKEY)
#    def testStartSession:
#        self.session=S3session('oregon')
#        session.setCredential
#        print self.host
    def testListBuckets(self):
        print "test List Bucktes\n===================================================="
        buckets=self.session.buckets()
        for bucket in buckets:
            print bucket.name
    def testCreateBucket(self):
        print "test create New Bucket\n===================================================="
        bucket=self.session.createBucket('feignwolf-test4')
    def testDeleteBucket(self):
        print "test delete Bucket\n===================================================="
        bucket=self.session.deleteBucket('feignwolf-test4')


#        self.bucket.info(fname)
if __name__ == '__main__':
    unittest.main()

